package com.huilian.iotbox.admin.wap.service.impl;

import com.huilian.iotbox.admin.wap.dao.DeviceAnnualFeeDao;
import com.huilian.iotbox.admin.wap.service.DeviceAnnualFeeService;
import com.huilian.iotbox.data.common.RedisKey;
import com.huilian.iotbox.data.dto.DeviceAnnualFeeDto;
import com.huilian.iotbox.data.po.DeviceAnnualFee;
import com.huilian.iotbox.data.utils.DateTimeUtils;
import com.huilian.iotbox.data.vo.DeviceAnnualFeeVo;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: loong
 * @create: 2024-06-24 10:32
 **/
@Service
public class DeviceAnnualFeeServiceImpl implements DeviceAnnualFeeService {
    @Resource
    private DeviceAnnualFeeDao deviceAnnualFeeDao;
    @Autowired
    private RedisTemplate<String, Serializable> redisCacheTemplate;

    @Override
    public List<DeviceAnnualFeeVo> findByUserId(UserInfo userInfo) {
        return deviceAnnualFeeDao.findByUserId(userInfo);
    }

    @Override
    public List<DeviceAnnualFeeVo> findStatisticsById(DeviceAnnualFeeDto deviceAnnualFeeDto) {
        return deviceAnnualFeeDao.findStatisticsById(deviceAnnualFeeDto);
    }

    @Override
    public List<DeviceAnnualFeeVo> findByIdList(List<Integer> idList) {
        return deviceAnnualFeeDao.findByIdList(idList);
    }

    @Override
    public List<DeviceAnnualFeeVo> findByDeviceAnnualFeeRecordId(Integer deviceAnnualFeeRecordId) {
        return deviceAnnualFeeDao.findByDeviceAnnualFeeRecordId(deviceAnnualFeeRecordId);
    }

    @Override
    public void update(DeviceAnnualFee deviceAnnualFee) {
        deviceAnnualFeeDao.update(deviceAnnualFee);
    }

    @Override
    public List<DeviceAnnualFeeVo> findExpireListByUserId(UserInfo userInfo) {
        List<DeviceAnnualFeeVo> returnList = new ArrayList<>();
        String key = String.format(RedisKey.HOME_USERID, userInfo.getId());
        Integer number = (Integer) redisCacheTemplate.opsForValue().get(key);
        if (number == null) {
            number = 0;
        }
        //一天只弹出一次框
        if (number > 0 ) {
            return returnList;
        }

        List<DeviceAnnualFeeVo> deviceAnnualFeeVoList = deviceAnnualFeeDao.findByUserId(userInfo);
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        for (DeviceAnnualFeeVo deviceAnnualFeeVo : deviceAnnualFeeVoList) {

            LocalDate localDate = deviceAnnualFeeVo.getDueDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            long day = DateTimeUtils.periodDays(currentDate,localDate);
            System.out.println("离过期的天数：" + day);
            if (day <= 30) {
                deviceAnnualFeeVo.setExpirationDay(day);
                returnList.add(deviceAnnualFeeVo);
            }
        }
        // 计算当前时间到明天零点的时间间隔（秒数）
        long secondsUntilMidnight = secondsUntilMidnight();
        System.out.println("计算当前时间到明天零点的时间间隔：" + secondsUntilMidnight);
        redisCacheTemplate.opsForValue().set(key,number + 1, secondsUntilMidnight, TimeUnit.SECONDS);
        return returnList;
    }
    private long secondsUntilMidnight() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime midnight = now.toLocalDate().plusDays(1).atStartOfDay();
        Duration duration = Duration.between(now, midnight);
        return duration.getSeconds();
    }
}
